package com.shine.app.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.adapter.MyFavoriteAdapter;
import com.shine.app.bean.MyFavoriteBean;
import com.shine.app.bean.UserBean;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfoFavoriteActivity extends BaseActivity implements
		OnClickListener {

	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;
	@InjectView(R.id.head_number)
	private TextView head_numberTextView;
	@InjectView(R.id.listview)
	private ListView listView;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private GetMyFavoriteAsyncTask getMyFavoriteAsyncTask;
	private MyFavoriteAdapter adapter = null;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				UIHelper.showHome(ViewMyInfoFavoriteActivity.this);
				overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewmyinfo);
		Injector.get(this).inject();
		init();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void init() {
		List<MyFavoriteBean> list = new ArrayList<MyFavoriteBean>();
		adapter = new MyFavoriteAdapter(this, list, imageLoader, handler);
		listView.setAdapter(adapter);
		head_nameTextView.setText(R.string.view_favorites);
		head_backImageView.setOnClickListener(this);
	}

	private void initData() {
		UserBean user = appContext.getLoginInfo().getUser();
		getMyFavoriteAsyncTask = new GetMyFavoriteAsyncTask();
		getMyFavoriteAsyncTask.execute(user.getGolferId());
	}

	public class GetMyFavoriteAsyncTask extends
			AsyncTask<Object, Integer, List<MyFavoriteBean>> {
		private String golferId;

		@Override
		protected List<MyFavoriteBean> doInBackground(Object... params) {
			List<MyFavoriteBean> list = null;
			golferId = (String) params[0];
			list = appContext.getMyFavoriteCourses(golferId);
			if (list == null) {
				list = new ArrayList<MyFavoriteBean>();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<MyFavoriteBean> result) {
			super.onPostExecute(result);
			head_numberTextView.setText("(" + result.size() + ")");
			progressBar.setVisibility(View.GONE);
			adapter.setData(result);
			adapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		AppManager.getAppManager().finishActivity();
		overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
	}

	@Override
	protected void onDestroy() {
		if (getMyFavoriteAsyncTask != null
				&& getMyFavoriteAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			getMyFavoriteAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
